/***************************************************************************
 *   Copyright (C) 2009 by MJ   *
 *   hanfman_cool@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//steamserverutil is a program to manage the steam game servers from hldsupdatetool.bin
//v0.3.1.1, last changes: 16.07.09 12:28


#include <iostream> //for cout and cin
#include <fstream> //for ifstream and ofstream
#include <stdlib.h> //for system()	
#include <string.h> //for strcpy, strcat and strcmp

#include "steamserverutil.h"
#include "screen.h"
#include "conf_server.h"



using namespace std;


//main menu
int main(int argc, char *argv[])
{
	system("clear;clear;clear"); //for a better look ;)
	
	char choice;

	cout << "steamserverutil " << _version << ", steamserverutil.googlecode.com\n";	
	

	ifstream test_file;
	strcpy(param, getenv("HOME"));
	strcat(param, "/.steamserverutil/workdir");
	test_file.open(param);
	if(test_file.is_open())
	{
		test_file.getline(workdir, 200);
	}
	else
		strcpy(workdir, ".steamserverutil");	//workdir is set to current dir = default
	
	test_file.close();
	
	
	//set workdir path
	strcpy(param, workdir);
	strcat(param, "/hldsupdatetool.bin");
	
	test_file.open(param);

	if(test_file.is_open())
	{
		cout << "found hldsupdatetool.bin\n";
	}
	else
	{
		
		cout << "Creating directory...\ndownloading hldsupdatetool.bin with wget...\n\n";
		system("mkdir .steamserverutil");
		system("cd .steamserverutil && wget http://www.steampowered.com/download/hldsupdatetool.bin && chmod +x hldsupdatetool.bin");
	}
	test_file.close();

	
	//set workdir path
	strcpy(param, workdir);
	strcat(param, "/steam");
	
	test_file.open(param);
	
	if( !test_file.is_open() )
	{
		cout << "\n\nFirst you have to accept the license agreement, show now?(y|n) ";
		cin >> choice;

		cout << "\n\n";
	
		if (choice == 'y')
		{
			strcpy(param, "cd ");
			strcat(param, workdir);
			strcat(param, " && ./hldsupdatetool.bin");
			system(param);
		}
		else
		{
			cout << "Aborting...\n";
			return 2;
		}	
			
	}
	
	

	test_file.close();
	
	//set workdir path
	strcpy(param, workdir);
	strcat(param, "/steam");
	
	test_file.open(param);

	if( test_file.is_open())
	{
		cout << "found steam\n";
		cout << "Current workdir is \"" << workdir << "\"";
		test_file.close();
		
		//now checking parameters
		if(argc == 3)
		{
			if(!strcmp(argv[1], "--exec-fav"))
			{
				int temp;
				sscanf(argv[2], "%d", &temp);
				favorites(temp);
				return 0;
			}
		}
		
		bool exit=false;

		
		while(!exit)
		{
			unsigned int menu;
			cout << "\n\n\t**main menu**\n";
			cout << "1.  Install or update game server\n";
			cout << "2.  Start game server\n";
			cout << "3.  Currently running servers with \"screen\"\n";
			cout << "4.  Favorites\n";
			cout << "5.  Configure\n";
			cout << "6.  Update game servers (verifys all data!)\n";
			cout << "7.  Update Steam Linux Client (recommend for first use)\n";
			cout << "8.  Delete servers\n";
			cout << "9.  About\n";
			cout << "10. Exit\n";
			cout << " : ";
			cin >> menu;
			
			cin.ignore();

			switch (menu)
			{
				case 1: 	install();
				break;
				case 2: 	start();
				break;
				case 3:		screen_test();
				break;
				case 4: 	favorites();
				break;
				case 5:		config_server();
				break;
				case 6:		update_server();
				break;
				case 7:		update_client();
				break;
				case 8: 	delete_server();
				break;
				case 9:		about();
				break;
				case 10: 	exit=true;
				break;
				default:	cout << "Try again ;)";
				break;
			}

		}
	}
	else
	{
		never_happen();
		cout << "\nUnknown Error!\n";
		return 1;
	}


	return 0;

}
